<?php
namespace haibao\www\data;

class Search extends BaseMysql{
	public function __construct(){
		parent::__construct('\haibao\www\model\data\Article');
	}
	
	public function searchBrandAndCelebrityByName($name){
		$data = array();
		$name = $this->escape($name);
		$sql = 'select * from (';
		$sql .= '(select NameCN name,UpdateTime from Brand where NameCN like "%'.$name.'%" order by UpdateTime DESC limit 0,10)';
		$sql .= 'union all';
		$sql .= '(select NameEN name,UpdateTime from Brand where NameEN like "%'.$name.'%" order by UpdateTime DESC limit 0,10)';
		$sql .= 'union all';
		$sql .= '(select NameCN name,UpdateTime from Celebrity where NameCN like "%'.$name.'%" order by UpdateTime DESC limit 0,10)';
		$sql .= 'union all';
		$sql .= '(select NameEN name,UpdateTime from Celebrity where NameEN like "%'.$name.'%" order by UpdateTime DESC limit 0,10)';
		$sql .= ') a order by UpdateTime desc limit 0,10';
		$result = $this->query($sql);
		while ($row = $result->fetch_assoc()){
			array_push($data,array('name'=>$result->getData('name')));
		}
		
		return $data;
	}
	
	public function getSearchWordSimilar($word,$pageSize = 10){
		$data = array();
		$db = $this->getDb();
		$name = $db->escape_string($word);
		$sql = 'select Keyword from SphinxKeyword where Keyword like "'.$name.'%" limit '.$pageSize;
		$result = $this->query($sql);
		while ($result->fetch_assoc()){
			array_push($data,$result->getData('Keyword'));
		}
		
		return $data;
	}
	
	public function isOwnWebKeyword($word){
		$isOwn = false;
		$sql = 'select Id from SphinxKeyword where Keyword=%s';
		$result = $this->query($sql,array($word));
		if ($result->fetch_assoc()){
			$isOwn = true;
		}
		
		return $isOwn;
	}
}